package com.example.myapplication;


import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class MainActivity extends Activity {
    private volatile int dir = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(()->{
//           allsquareblack();
        }).start();
        int[][] arr = increatenum();
        LinearLayout layout = findViewById(R.id.lay1);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dir++;
            }
        });
        new Thread(()->{
            while (true)
            {
                int rm = new Random().nextInt(7);
                getSquare1(arr[rm]);
                dropDown(arr[rm],rm);
                getSquare();
            }
        }).start();
    }

    protected Map createsquare()
    {
        Map map = new HashMap();
        int[] nextsq = new int[8];
        int[] nextsqin = new int[8];
        nextsq[0] = R.id.next1;
        nextsq[1] = R.id.next2;
        nextsq[2] = R.id.next3;
        nextsq[3] = R.id.next4;
        nextsq[4] = R.id.next5;
        nextsq[5] = R.id.next6;
        nextsq[6] = R.id.next7;
        nextsq[7] = R.id.next8;
        nextsqin[0] = R.id.next1i;
        nextsqin[1] = R.id.next2i;
        nextsqin[2] = R.id.next3i;
        nextsqin[3] = R.id.next4i;
        nextsqin[4] = R.id.next5i;
        nextsqin[5] = R.id.next6i;
        nextsqin[6] = R.id.next7i;
        nextsqin[7] = R.id.next8i;
        map.put("ns",nextsq);
        map.put("nsi",nextsqin);
        return map;
    }

    protected Map findSuqare()
    {
        Map map = new HashMap();
        String[][] square = new String[10][22];
        String[][] insquare = new String[10][22];
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 22; j++) {
                square[i][j] = "";
            }
        }
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 22; j++) {
                insquare[i][j] = "";
            }
        }
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 22; j++) {
                square[i][j] += "s"+Integer.toString(i+1)+"q"+Integer.toString(j+1);
            }
        }
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 22; j++) {
                insquare[i][j] += "s"+Integer.toString(i+1)+"q"+Integer.toString(j+1)+"i";
            }
        }
        map.put("square",square);
        map.put("insquare",insquare);
        return map;
    }

    protected void allsquareblack()
    {
        Map map = findSuqare();
        String[][] sq = (String[][]) map.get("square");
        String[][] insq = (String[][]) map.get("insquare");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 22; i > 0; i--) {
            String[] s = getRow(sq,i);
            String[] si = getRow(insq,i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int j = 0; j < s.length; j++) {
                try {
                    changeBackground(s[j],si[j],1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected void changeBackground(String sq,String insq,int way)
    {
        Class c = R.id.class;
        int id1 = 0;
        int id2 = 0;
        try {
            Field f = c.getField(sq);
            Field f1 = c.getField(insq);
            id1 = (int)f.get(c);
            id2 = (int) f1.get(c);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        TextView textView = findViewById(id1);
        switch (way)
        {
            case 1:textView.setBackgroundResource(R.drawable.shape_square2);break;
            default:textView.setBackgroundResource(R.drawable.shape_square);break;
        }
        TextView textView1 = findViewById(id2);
        switch (way)
        {
            case 1:textView1.setBackgroundResource(R.drawable.shape_square3);break;
            default:textView1.setBackgroundResource(R.drawable.shape_square1);break;
        }
    }

    protected String[] getRow(String[][] square,int n)
    {
        String[] newstr = new String[10];
        int index = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 21; j >= 0; j--) {
                if (j == n-1)
                {
                    newstr[index++] = square[i][j];
                }
            }
        }
        return newstr;
    }

    protected void getSquare()
    {
        Map map = createsquare();
        int[] ns = (int[]) map.get("ns");
        int[] nsi = (int[]) map.get("nsi");
        for (int i = 0; i < ns.length; i++) {
            TextView textView = findViewById(ns[i]);
            textView.setBackgroundResource(R.drawable.shape_square);
            TextView textView1 = findViewById(nsi[i]);
            textView1.setBackgroundResource(R.drawable.shape_square1);
        }
    }

    protected void getSquare1(int[] arr)
    {
        Map map = createsquare();
        int[] ns = (int[]) map.get("ns");
        int[] nsi = (int[]) map.get("nsi");
        for (int i = 0; i < ns.length; i++) {
            if (i == arr[0] || i == arr[1] || i == arr[2] || i == arr[3])
            {
                TextView textView = findViewById(ns[i]);
                textView.setBackgroundResource(R.drawable.shape_square2);
                TextView textView1 = findViewById(nsi[i]);
                textView1.setBackgroundResource(R.drawable.shape_square3);
            }
        }
    }

    protected int[][] increatenum()
    {
        int[][] arr = new int[7][4];
        arr[0][0] = 2;
        arr[0][1] = 1;
        arr[0][2] = 5;
        arr[0][3] = 4;

        arr[1][0] = 2;
        arr[1][1] = 1;
        arr[1][2] = 5;
        arr[1][3] = 6;

        arr[2][0] = 2;
        arr[2][1] = 1;
        arr[2][2] = 6;
        arr[2][3] = 7;

        arr[3][0] = 0;
        arr[3][1] = 6;
        arr[3][2] = 5;
        arr[3][3] = 4;

        arr[4][0] = 7;
        arr[4][1] = 6;
        arr[4][2] = 5;
        arr[4][3] = 4;

        arr[5][0] = 2;
        arr[5][1] = 6;
        arr[5][2] = 5;
        arr[5][3] = 4;

        arr[6][0] = 2;
        arr[6][1] = 6;
        arr[6][2] = 5;
        arr[6][3] = 7;
        return arr;
    }

    protected void dropDown(int[] arr,int r)//
    {
        Map map = findSuqare();
        String[][] sq = (String[][]) map.get("square");
        String[][] insq = (String[][]) map.get("insquare");
        String[] sq1 = new String[8];
        String[] insq1 = new String[8];
        int index = 0,line = 21;
        for (int i = 0; i < line; i++) {//21 20 19 18
            for (int j = 0; j < 10; j++) {
                if(dir == 0)
                {
                    line = 21;
                    if (j == 3 || j == 4 || j == 5 || j == 6)
                    {
                        sq1[index] = sq[j][i];
                        insq1[index] = insq[j][i];
                        sq1[index+4] = sq[j][i+1];
                        insq1[index+4] = insq[j][i+1];
                        index++;
                    }
                }else if (dir == 2)
                {
                    line = 21;
                    if (r == 4)
                    {
                        if (j == 3 || j == 4 || j == 5 || j == 6)
                        {
                            sq1[3-index] = sq[j][i];
                            insq1[3-index] = insq[j][i];
                            sq1[7-index] = sq[j][i+1];
                            insq1[7-index] = insq[j][i+1];
                            index++;
                        }
                    }
                    else
                    {
                        if (j == 3 || j == 4 || j == 5 || j == 6)
                        {
                            sq1[7-index] = sq[j][i];
                            insq1[7-index] = insq[j][i];
                            sq1[3-index] = sq[j][i+1];
                            insq1[3-index] = insq[j][i+1];
                            index++;
                        }
                    }

                }else if (dir == 1)
                {
                    line = 19;
                    if (j == 4 || j == 5)
                    {
                        if (r == 0 || r == 1 || r == 3 || r == 5)
                        {
                            if (j == 5)
                            {
                                sq1[index+3] = sq[j][i];
                                insq1[index+3] = insq[j][i];
                                sq1[index] = sq[j][i+1];
                                insq1[index] = insq[j][i+1];
                                sq1[index+1] = sq[j][i+2];
                                insq1[index+1] = insq[j][i+2];
                                sq1[index+2] = sq[j][i+3];
                                insq1[index+2] = insq[j][i+3];
                            }else
                            {
                                sq1[index+7] = sq[j][i];
                                insq1[index+7] = insq[j][i];
                                sq1[index+4] = sq[j][i+1];
                                insq1[index+4] = insq[j][i+1];
                                sq1[index+5] = sq[j][i+2];
                                insq1[index+5] = insq[j][i+2];
                                sq1[index+6] = sq[j][i+3];
                                insq1[index+6] = insq[j][i+3];
                            }
                        }else
                        {
                            if (j == 5)
                            {
                                sq1[index] = sq[j][i];
                                insq1[index] = insq[j][i];
                                sq1[index+1] = sq[j][i+1];
                                insq1[index+1] = insq[j][i+1];
                                sq1[index+2] = sq[j][i+2];
                                insq1[index+2] = insq[j][i+2];
                                sq1[index+3] = sq[j][i+3];
                                insq1[index+3] = insq[j][i+3];
                            }else
                            {
                                sq1[index+4] = sq[j][i];
                                insq1[index+4] = insq[j][i];
                                sq1[index+5] = sq[j][i+1];
                                insq1[index+5] = insq[j][i+1];
                                sq1[index+6] = sq[j][i+2];
                                insq1[index+6] = insq[j][i+2];
                                sq1[index+7] = sq[j][i+3];
                                insq1[index+7] = insq[j][i+3];
                            }
                        }
                    }
                }else if (dir == 3)
                {
                    line = 19;
                    if (j == 4 || j == 5)
                    {
                        if (r == 1 || r == 2 || r == 6)
                        {
                            if (j == 4)
                            {
                                sq1[index] = sq[j][i];
                                insq1[index] = insq[j][i];
                                sq1[index+3] = sq[j][i+1];
                                insq1[index+3] = insq[j][i+1];
                                sq1[index+2] = sq[j][i+2];
                                insq1[index+2] = insq[j][i+2];
                                sq1[index+1] = sq[j][i+3];
                                insq1[index+1] = insq[j][i+3];
                            }else
                            {
                                sq1[index+4] = sq[j][i];
                                insq1[index+4] = insq[j][i];
                                sq1[index+7] = sq[j][i+1];
                                insq1[index+7] = insq[j][i+1];
                                sq1[index+6] = sq[j][i+2];
                                insq1[index+6] = insq[j][i+2];
                                sq1[index+5] = sq[j][i+3];
                                insq1[index+5] = insq[j][i+3];
                            }
                        }else
                        {
                            if (j == 4)
                            {
                                sq1[index+3] = sq[j][i];
                                insq1[index+3] = insq[j][i];
                                sq1[index+2] = sq[j][i+1];
                                insq1[index+2] = insq[j][i+1];
                                sq1[index+1] = sq[j][i+2];
                                insq1[index+1] = insq[j][i+2];
                                sq1[index] = sq[j][i+3];
                                insq1[index] = insq[j][i+3];
                            }else
                            {
                                sq1[index+7] = sq[j][i];
                                insq1[index+7] = insq[j][i];
                                sq1[index+6] = sq[j][i+1];
                                insq1[index+6] = insq[j][i+1];
                                sq1[index+5] = sq[j][i+2];
                                insq1[index+5] = insq[j][i+2];
                                sq1[index+4] = sq[j][i+3];
                                insq1[index+4] = insq[j][i+3];
                            }
                        }
                    }
                }else
                {
                    dir = 0;
                }
            }
            index = 0;
            for (int k = 0; k < sq1.length; k++) {
                if (k == arr[0] || k == arr[1] || k == arr[2] || k == arr[3])
                {
                    changeBackground(sq1[k],insq1[k],1);
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (i != line-1)
            {
                for (int k = 0; k < sq1.length; k++) {
                    if (k == arr[0] || k == arr[1] || k == arr[2] || k == arr[3])
                    {
                        changeBackground(sq1[k],insq1[k],0);
                    }
                }
            }
        }
    }
}